'use strict';
module.exports = app => {
  const { STRING, INTEGER } = app.Sequelize;
  const Material = app.model.define(
      'Material',
      {
          id: {type: INTEGER, primaryKey: true, autoIncrement: true},
          category_id: INTEGER(11),
          name: STRING(255),
          desc: STRING(255),
          status: INTEGER(11),
          upload_type: INTEGER(11),
          img: {
                type: STRING(255),
                get() {
                    const img = this.getDataValue('img');
                    if (img && (img.includes('http://') || img.includes('https://'))) {
                        return img;
                    }
                    return app.config.imgHost + img;
                }
          }
      },
      {
          tableName: 'material',
          timestamps: true, // 启用时间戳(createdAt, updatedAt)
          updatedAt: 'updated_time', // 不想要 updatedAt
          createdAt: 'create_time', // 想要  createdAt 但是希望名称叫做 created_at
      }
  );

    Material.associate = function() {
        app.model.Material.belongsTo(app.model.Category, {foreignKey: 'category_id', targetKey: 'id', as: 'category'});
    }
    return Material
};
